package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.GoodsMapper;
import com.hqyj.dao.ReserveMapper;
import com.hqyj.dao.SysTableMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.Reserve;
import com.hqyj.pojo.SysTable;
import com.hqyj.service.ReserveService;
import com.hqyj.utile.ColorUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class ReserveServiceImpl implements ReserveService {
    @Autowired
    ReserveMapper reserveMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SysTableMapper sysTableMapper;

    @Autowired
    GoodsMapper goodsMapper;
    public HashMap<String, Object> select(Goods goods, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //查询当前共有多少条数据
        //查询当前还有条已处理数据
        int countAll = goodsMapper.countNumberAll();
        int countLots = goodsMapper.countNumberLots();

        System.out.println(countAll+"!!!!!!!!???????????!!!!!!!!!"+countLots);
        //设置有多少数据待处理
        request.setAttribute("countAll",countAll);
        request.setAttribute("countLots",countLots);
        HttpSession session=request.getSession();
        session.setAttribute("countAll",countAll);
        session.setAttribute("countLots",countLots);

        //模糊查询判断,传入参数是想要去做模糊查询
        if(goods.getgId()!=null){
            goods.setgId(goods.getgId());
        }
        if(goods.getgUserId()!=null){
            goods.setUserId(goods.getUserId());
        }
        System.out.println("emmmmmmmmmmmmmmmmm"+goods);
        //分页
        //1 设置分页查询的页码，每页显示的行数
        PageHelper.startPage(goods.getPage(), goods.getRow());
        //2 查询
        System.out.println("======================" + goods.getField()+"========================");
        List<Goods> list = goodsMapper.selectAll(goods);
        System.out.println("真的运行了吗"+list.size());
        System.out.println("==============================================");

        if (goods.getField()!=null){
            List<Goods> listColor = ColorUtil.updateReviewTableColor(list);
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(listColor);
            if(list.size()==0){
                map.put("info","没有查询到数据");
            }else{
                //获取当前页的集合
                map.put("info",pageInfo.getList());
                //总条数
                Long  total = pageInfo.getTotal();
                //总页数
                int  totalPage  = pageInfo.getPages();
                System.out.println("上一页="+pageInfo.getPrePage());
                System.out.println("下一页="+pageInfo.getNextPage());
                map.put("total",total);
                map.put("totalPage",totalPage);
//            map.put("style1","style='background-color: #33bbdd'");
            }
        }else{
            List<Goods> listColor = ColorUtil.updateReserveColor1(list);
            System.out.println("1234567890额呵呵呵"+listColor);
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(listColor);
            if(list.size()==0){
                map.put("info","没有查询到数据");
            }else{
                //获取当前页的集合
                map.put("info",pageInfo.getList());
                //总条数
                Long  total = pageInfo.getTotal();
                //总页数
                int  totalPage  = pageInfo.getPages();
                System.out.println("上一页="+pageInfo.getPrePage());
                System.out.println("下一页="+pageInfo.getNextPage());
                map.put("total",total);
                map.put("totalPage",totalPage);
//            map.put("style1","style='background-color: #33bbdd'");
            }
        }

        return map;
    }

    public Reserve selectById(Integer tId, Integer userId) {
        System.out.println("当前的tid为:"+tId+"当前的userId为:"+userId);
        return reserveMapper.selectByPrimaryKey(tId,userId);
    }

    public int selectNumber(Reserve reserve) {

        return sysTableMapper.seleceTableById(reserve.gettId());
    }

    public HashMap<String, Object> update(Reserve reserve) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println("===============当前时间为：==============="+reserve);
        int success= reserveMapper.updateByPrimaryKeySelective(reserve);
        if (success ==1){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    public HashMap<String, Object> deleteReserveBytId(Reserve reserve) {

        HashMap<String, Object> map = new HashMap<String, Object>();

        List<Reserve> listReserve = reserveMapper.selectAllReserves1(reserve);
        ColorUtil.updateTableState(listReserve,sysTableMapper, reserveMapper,reserve);
        int success= reserveMapper.deleteByPrimaryKey(reserve);
        if (success ==1){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    public HashMap<String, Object> updateTableState(Reserve reserve) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        //获取userId
        int userId = reserve.getUserId();
        //除了修改Table的状态之外，还要修改它们的按钮颜色状态
        sysTableMapper.updateTableState1(reserve.gettId());
        reserveMapper.updateTableState111(reserve.gettId(),reserve.getUserId());
        map.put("info1",userId);
        return map;
    }

    public int selectUserIdByUserName(String userName) {
        int success = userMapper.selectUserIdByName(userName);
        return success;
    }

    public HashMap<String, Object> updateViewState(Goods goods) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //获取userId
        System.out.println("====================================="+goods+"=====================================");
        int userId = goods.getgUserId();
        String gId = goods.getgId();
        System.out.println("===============emmmmm=========="+userId+"====="+gId);
        goodsMapper.updateTableState1(gId,userId);
        map.put("info1","更改成功");
        return map;
    }
    public HashMap<String, Object> updateViewState1(Goods goods) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //获取userId
        System.out.println("====================================="+goods+"=====================================");
        int userId = goods.getgUserId();
        String gId = goods.getgId();
        System.out.println("===============emmmmm=========="+userId+"====="+gId);
        goodsMapper.updateTableState11(gId,userId);
        map.put("info1","更改成功");
        return map;
    }
}
